package com.jwgf.smarowindingmachine_unity.logic;

import com.jwgf.Half485.modbusthreadpool.ModbusUtils;
import com.jwgf.common.Utility;
import com.jwgf.Half485.fxthreadpool.FxUtils;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Jacky on 2017/12/15.
 */

public class CommandGenerator {
    private static String plcType = MachineParameterSharedPreference.getInstance().getValue(MachineParameterSharedPreference.PLC_TYPE, "0");
    private static HashMap<Integer, Integer> addressMap;

    public static byte[] createWriteCommand(int id, int address, byte[] data) {
        init();
        byte[] cmd;
        if (plcType.equals("0")) {
            cmd = ModbusUtils.createWriteRegisterCmd((byte) id, address, data);
        } else {
            short temp = 0;
            if (data.length == 2) {
                temp = Utility.ByteArrayUtils.byteArray2Short(data);
            } else if (data.length == 4) {
                temp = (short) Utility.ByteArrayUtils.byteArray2Int(data);
            }
            cmd = FxUtils.createWWCmd(getId(address), getAddress(address), temp);
        }

        return cmd;
    }

    //同时向多个站点写命令，需要在函数内进行特殊处理
    public static ArrayList<byte[]> createWriteCommandList(int id, int address, byte[] data) {
        init();
        ArrayList<byte[]> cmdList = new ArrayList<>();
        if (plcType.equals("0")) {
            cmdList.add(ModbusUtils.createWriteRegisterCmd((byte) id, address, data));
        } else {
            short temp = 0;
            if (data.length == 2) {
                temp = Utility.ByteArrayUtils.byteArray2Short(data);
            } else if (data.length == 4) {
                temp = (short) Utility.ByteArrayUtils.byteArray2Int(data);
            }
            if (address == 100) {
                cmdList.add(FxUtils.createWWCmd(4, getAddress(address), temp));
                cmdList.add(FxUtils.createWWCmd(getId(address), getAddress(address), temp));
            }

        }

        return cmdList;
    }

    private static void init() {
        if (addressMap == null) {
            addressMap = new HashMap<Integer, Integer>();
            addressMap.put(0, 2 << 16 | 720);
            addressMap.put(1, 2 << 16 | 721);
            addressMap.put(2, 2 << 16 | 722);
            addressMap.put(3, 2 << 16 | 723);
            addressMap.put(4, 2 << 16 | 724);
            addressMap.put(5, 2 << 16 | 725);
            addressMap.put(6, 2 << 16 | 727);
            addressMap.put(10, 2 << 16 | 726);

            addressMap.put(50, 4 << 16 | 407);
            addressMap.put(51, 4 << 16 | 403);
            addressMap.put(52, 4 << 16 | 404);
            addressMap.put(53, 4 << 16 | 406);
            addressMap.put(54, 4 << 16 | 405);
            addressMap.put(55, 4 << 16 | 408);
            addressMap.put(56, 4 << 16 | 402);
            addressMap.put(57, 4 << 16 | 400);
            addressMap.put(58, 4 << 16 | 401);
            //更换品种
            addressMap.put(59, 4 << 16 | 409);
            addressMap.put(103, 4 << 16 | 409);

            addressMap.put(60, 4 << 16 | 416);
            addressMap.put(61, 4 << 16 | 415);
            addressMap.put(62, 4 << 16 | 417);
            addressMap.put(63, 4 << 16 | 420);
            addressMap.put(64, 4 << 16 | 411);

            addressMap.put(100, 5 << 16 | 412);
            addressMap.put(101, 5 << 16 | 416);
            addressMap.put(102, 5 << 16 | 411);

            addressMap.put(150, 6 << 16 | 210);
            addressMap.put(151, 6 << 16 | 211);
            addressMap.put(152, 6 << 16 | 212);
            addressMap.put(153, 6 << 16 | 213);
            addressMap.put(154, 6 << 16 | 214);
            addressMap.put(155, 6 << 16 | 215);
            addressMap.put(156, 6 << 16 | 216);
            addressMap.put(157, 6 << 16 | 224);
            addressMap.put(158, 6 << 16 | 225);
            addressMap.put(159, 6 << 16 | 226);

            addressMap.put(200, 8 << 16 | 2);
            addressMap.put(201, 8 << 16 | 3);
            addressMap.put(202, 8 << 16 | 4);
        }
    }

    private static int getId(int address) {
        return addressMap.get(address) >> 16;
    }

    private static int getAddress(int address) {
        return addressMap.get(address) & 0xffff;
    }

}
